<?php
/***************************************************************************
 * 
 * Copyright (c) 2010 , Inc. All Rights Reserved
 * $Id$:View.php,2010/05/11 13:45:47 
 * 
 **************************************************************************/
 
 
 
/**
 * @file View.php
 * @author huqingping
 * @date 2010/05/11 13:45:47
 * @version 1.0 
 * @brief 
 *  
 **/
require_once FR_ROOT.'view/IView.php';
require_once 'smarty/Smarty.class.php';

class SmartyView implements IView
{
	private $smarty = null;
	private $viewData = array();
	private $app = null;
	private $gpc = false;

	function init($app)
	{
		$this->app = $app;
		$this->gpc = get_magic_quotes_gpc();
		$this->smarty = new Smarty();
		$this->smarty->template_dir = PAGE_ROOT;
		$this->smarty->force_compile = Conf::get('hapn.smarty.forcecompile',true);
		$this->smarty->compile_dir  =  TMP_ROOT . 'smarty/compile';
		$this->smarty->cache_dir    =  TMP_ROOT . 'smarty/cache';
		$this->smarty->config_dir   =  PAGE_ROOT . 'conf';
		$this->smarty->caching      =  false;
		$this->smarty->left_delimiter   = '<%';
		$this->smarty->right_delimiter  = '%>';
		$this->smarty->debugging = $app?$app->debug:false;

		$methods = get_class_methods($this);
		//注册扩展函数
		foreach($methods as $method) {
			if (strncmp($method,'_ex_',4) === 0) {
				$this->smarty->register->templateFunction(substr($method,4),array($this,$method));
			}
		}
	}

	function setArray(Array $arr)
	{
		if ($this->viewData) {
			$this->viewData = array_merge($this->viewData,$arr);
		} else {
			$this->viewData = $arr;
		}
	}
	
	function assign($key, $value)
	{
		$this->viewData[$key] = $value;
	}

	function clear()
	{
		$this->viewData = array();
	}

	//对需要显示的数据做转义处理
	function normalizeData(&$v)
	{
		if (is_string($v)) {
			//不编码单双引号
			$flag = ENT_NOQUOTES;
			if (!$this->gpc){
				//如果php没有自动加上\，则但双引号都编码
				$flag = ENT_QUOTES;
			}
			$v = htmlspecialchars($v,$flag);
		} elseif (is_array($v)) {
			foreach($v as $k=>&$iv) {
				if (strncmp($k,'vr_',3) === 0 ||
					strncmp($k,'js_',3) === 0) {
					//这两种参数不转义
					continue;
				}
				$this->normalizeData($iv);
			}
		}
	}

	function build($template)
	{
//		$this->normalizeData($this->viewData);
		$this->smarty->assign('v', $this->viewData);
		return $this->smarty->fetch($template.'.tpl');
	}

	function display($template)
	{
//		$this->normalizeData($this->viewData);
		$this->smarty->assign('v', $this->viewData);
		$this->smarty->display($template.'.tpl');
	}

	function _ex_encid($params)
	{
		$crypt = MCUtil::load('Fcrypt');
		$key = isset($params['key']) ? $params['key'] : null;
		if ($key) {
			$ret = $crypt->encode($params['id'],$key);
		} else {
			$ret = $crypt->encode($params['id']);
		}
		if ( false==$ret ) { 
			return isset($params['default']) ? $params['default'] : ''; 
		} else {
			return $ret;
		} 
	}

	function _ex_enc2id($params)
	{
		$crypt = MCUtil::load('Fcrypt');
		$key = isset($params['key']) ? $params['key'] : null;
		if ($key) {
			$ret = $crypt->encode2Num(intval($params['id1']),intval($params['id2']),$key);
		} else {
			$ret = $crypt->encode2Num(intval($params['id1']),intval($params['id2']));
		}
		if ( false==$ret ) { 
			return isset($params['default']) ? $params['default'] : ''; 
		} else {
			return $ret;
		} 
	}

	function _ex_data2text($params)
	{   
		$ret = $params['data'];
		$ret = str_replace(array('<br />','<br/>'), "\n", $ret);
		return $ret;
	} 

	function _ex_trunc_text($params)
	{
		$text = $params['data'];
		$count = $params['count'];
		if (strlen($text) <= $count) {
			return $text;
		}   
		$len = $count-3;
		$ret = substr($text, 0, $len);
		$ret = fix_lastchr($ret);
		$ret .= '...';
		return $ret;
	}

	function _ex_gbk2jsunicode($params)
	{
		$uconv_invchar_replace = 0x1;
		if (!isset($params['data'])) {
			return '';
		}
		$text = $params['data'];
		if (is_string($text) && strlen($text)==0) {
			return '';
		}
		$content = gbk_to_jsunicode($text, $uconv_invchar_replace);
		if($content === false ){
			$content = $text;
		}
		return $content;
	}
}






/* vim: set ts=4 sw=4 sts=4 tw=100 noet: */
